package com.example.nep.base;

/**
 * @Author: 韩建平
 * @Description: TODO
 * @DateTime: 2024/4/2 11:15
 **/

import com.alibaba.fastjson2.JSONObject;

public class R {
    private Integer code;
    private String msg;
    private Object data;


    /**
     * 虚拟字段，用于前端判断响应结果的状态。
     * @return
     */
    public boolean isOk(){
        if(this.code==200){
            return true;
        }
        return false;
    }



    public static R ok(){
        R r=new R();
        r.code=200;
        r.msg="请求成功";
        return r;
    }

    public static R ok(String msg){
        R r=new R();
        r.code=200;
        r.msg=msg;
        return r;
    }
    public static R error(){
        R r=new R();
        r.code=500;
        r.msg="请求失败";
        return r;
    }
    public static R error(String msg){
        R r=new R();
        r.code=500;
        r.msg=msg;
        return r;
    }
    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public Object getData() {
        return data;
    }


    public R setCode(Integer code) {
        this.code = code;
        return this;
    }
    public R setMsg(String msg) {
        this.msg = msg;
        return this;
    }
    public R setData(Object data) {
        this.data = data;
        return this;
    }
    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

    public R setResult(String key,Object value){
        if(data==null){
            JSONObject data=new JSONObject();
            data.put(key,value);
            this.data=data;
        }else{
            if(data instanceof JSONObject){
                ((JSONObject)data).put(key,value);
            }
        }
        return this;
    }
}

